package stage1.operation.module3;

import java.util.*;

/**
 * task 1,2,3作业在这里
 */
public class Task {
    public static void main(String args[]) {
        task1();
        task2();
        task3();
    }

    /**
     * 1. 编程统计字符串"ABCD123!@#$%ab"中大写字母、小写字母、数字、其它字符的个数并打 印出来。
     */
    public static void task1() {
        System.out.println("请输入要统计的字符串：");
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int uppercaseLetter = 0;
        int lowerCaseLetters = 0;
        int digital = 0;
        int otherCharacters = 0;
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                uppercaseLetter++;
            } else if (ch >= 'a' && ch <= 'z') {
                lowerCaseLetters++;
            } else if (ch >= '0' && ch <= '9') {
                digital++;
            } else {
                otherCharacters++;
            }
        }

        System.out.println(s + "中大写字母有:" + uppercaseLetter + "个,小写字母有:" + lowerCaseLetters + "个,数字字符:"
                + digital + "个,其他字符:" + otherCharacters + "个");
    }

    /**
     * 2. 编程获取两个指定字符串中的最大相同子串。
     * <p>
     * 如： s1="asdafghjka", s2="aaasdfg" 他们的最大子串为"asd"
     * <p>
     * 提示： 将短的那个串进行长度依次递减的子串与较长的串比较。
     */
    public static void task2() {
        String s1 = "asdafghjka";
        String s2 = "aaasdfg";
        String longString = "";
        String shortString = "";
        if (s1.length() > s2.length()) {
            longString = s1;
            shortString = s2;
        } else {
            longString = s2;
            shortString = s1;
        }

        for (int x = 0; x < shortString.length(); x++) {
            for (int y = 0, z = shortString.length() - x; z != shortString.length() + 1; y++, z++) {
                String temp = shortString.substring(y, z);
                if (longString.contains(temp)) {
                    System.out.println("s1：" + s1 + "和s2：" + s2 + "最大相同子串：" + temp);
                    return;
                }
            }
        }
        System.out.println("s1：" + s1 + "和s2：" + s2 + "没有相同子串");
    }

    /**
     * 3. 准备一个 HashMap 集合，统计字符串"123,456,789,123,456"中每个数字字符串出现的次数并打印出来。
     * <p>
     * 如：
     * <p>
     * 123 出现了 2 次
     * <p>
     * 456 出现了 2 次
     * <p>
     * 789 出现了 1 次
     */
    public static void task3() {
        Map<String, Integer> m1 = new HashMap<String, Integer>();
        String str1 = "123,456,789,123,456";
        String[] sArr = str1.split(",");
        for (int i = 0; i < sArr.length; i++) {
            if (!m1.containsKey(sArr[i])) {
                m1.put(sArr[i], 1);
            } else {
                m1.put(sArr[i], m1.get(sArr[i]) + 1);
            }
        }
        Set<Map.Entry<String, Integer>> s1 = m1.entrySet();
        for (Map.Entry<String, Integer> me1 : s1) {
            System.out.println(me1.getKey() + "出现了" + me1.getValue() + "次！");
        }
    }
}
